import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingsRouter from './modules/settings'
import socialRouter from './modules/social'
// 页面中一些页面是白名单页面, 任何用户都可以访问的页面 这些页面的路由 称为 静态路由(constantRoutes没有权限的路由)
// 剩余的页面(员工,组织架构,审批....)是涉及到权限的页面, 称这些路由为 动态路由(asyncRoutes有权限的路由)
// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    // hidden: true, // 控制是否在左侧导航菜单中显示, 如果一个父链接中只展示一个子链接,直接展示子链接
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      // title是控制导航的文本 icon是控制导航的图标
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    // hidden: true,
    path: '/import',
    component: Layout,
    children: [{
      path: '',
      name: 'import',
      component: () => import('@/views/import/index')
    }]
  }
]
// 动态路由
export const asyncRoutes = [
  departmentsRouter,
  settingsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  approvalsRouter,
  socialRouter
]
// 开发阶段处于的是脚手架环境(Webpack环境), 但是项目上线 需要打包, 打包出来的dist目录放到服务器，如果服务器不做支持 是会白屏
const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  //
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
